package
{
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_input;
	import UIFree.uif_Timer;
	import UIFree.uif_app;
	import UIFree.uif_styles;
	
	import descentFly.app.df_styles;
	import descentFly.away3D4.net.mySocket;
	import descentFly.controller.df_userController;
	import descentFly.server.df_servers;
	import descentFly.server.myserver;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.events.TimerEvent;
	import flash.geom.Matrix3D;
	import flash.geom.Rectangle;
	import flash.geom.Vector3D;
	import flash.net.Socket;
	import flash.system.Security;
	
	[SWF(width="700", height="500", frameRate="40", backgroundColor="#FFFFFF")]
	public class fly_serverTest extends uif_app {
		private var _myserver	: myserver;
		private var _socket		: mySocket;
		private var _echo		: uif_input;
		
		public function fly_serverTest() {
			Security.loadPolicyFile('http://vmaya.ru/crossdomain.xml');
			Security.loadPolicyFile('xmlsocket://vmaya.ru:843');
			Security.allowDomain('*');
			
			super(new df_styles());
			styleClass = 'application';
			resize(700, 500);
			
			var inner: Rectangle = getInner();
			_echo = new uif_input(this, inner.x, inner.y, inner.width / 2, inner.height);
			
			_socket = new mySocket(Math.ceil(Math.random() * 100), 10001, null);
			_socket.connect(df_servers.HOST, 1077);
			
			_socket.addEventListener(Event.COMPLETE, doComplete);
			_socket.addEventListener(mySocket.RESPONSE_NOTIFY, doNotify);
			_socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			_socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
			_socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
			//Security.loadPolicyFile(df_servers.HOST_URL);
		}
		
		protected function get myChipMatrix(): Matrix3D {
			var matrix		: Matrix3D = new Matrix3D();
			matrix.appendRotation(Math.random(), Vector3D.X_AXIS);
			return matrix;
		}
		
		protected function doNotify(e: ObjectEvent): void {
			_echo.textField.appendText(e.object.notify + ', uid: ' + e.object.params.uid + '\n');
		}
		
		protected function doComplete(e: Event): void {
			var ticCount	: int = 30;
			var matrix		: Matrix3D = new Matrix3D();
			
			var _timer: uif_Timer = new uif_Timer(1000, function(e: TimerEvent): void {
				matrix.appendRotation(12, Vector3D.Y_AXIS);
				
				if (_timer.currentCount == ticCount) {
					_socket.closeConnection();
				}
				
				if (Math.random() > 0.4) _socket.updateAction(df_userController.ACTION_FORWARD, (Math.random()>0.5)?true:false);
				else _socket.getSessionState(function(data: Object): void {
					_echo.textField.appendText(data.toString());
				});
			}, null, ticCount);
		}
		
		private function ioErrorHandler(event: IOErrorEvent):void {
			trace("ioErrorHandler: " + event);
		}
		
		private function securityErrorHandler(event: SecurityErrorEvent):void {
			trace("securityErrorHandler: " + event);
		}
		
		private function socketDataHandler(event: ProgressEvent):void {
		}	
		
	}
}